package com.yinlixian.frame.config.handler;

import com.yinlixian.frame.entity.ResultInfo;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @ClassName: JsonResultResponseAdvice
 * @Description:
 * @author: yudm
 * @date: 2017-03-07 13:42
 */
@Order(1)
@ControllerAdvice(basePackages = "com.yinlixian.frame.admin.controller")
public class ResultInfoResponseAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType,
                            Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType,
                                  MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        ResultInfo<Object> result = new ResultInfo<>();
        result.setData(body);
        result.setResult(ResultInfo.Result.OK);
        return result;
    }
}
